# example code is taken from https://github.com/Akagi201/learning-cmake/tree/master/hello-world-lib
# for test only

load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
load("@rules_shell//shell:sh_test.bzl", "sh_test")

filegroup(
    name = "srcs",
    srcs = glob(["**"]),
    visibility = ["//visibility:public"],
)

# Shows a standard library
cmake(
    name = "libhello",
    generate_args = select({
        "//:windows": [
            "-G \"Visual Studio 15 2017\"",
            "-A x64",
        ],
        "//conditions:default": None,
    }),
    lib_source = ":srcs",
    # pass include/version123 to C/C++ provider as include directory
    out_include_dir = "include/version123",
)

cc_binary(
    name = "libhello_example",
    # includes just hello.h, include directory: "include/version123"
    srcs = ["hello_client.c"],
    deps = [":libhello"],
)

sh_test(
    name = "test_hello",
    srcs = ["test_hello.sh"],
    args = ["$(location libhello_example)"],
    data = [":libhello_example"],
    tags = ["windows"],
    visibility = ["//:__pkg__"],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

# Shows a standard library using the Ninja generator
cmake(
    name = "libhello_ninja",
    generate_args = ["-GNinja"],
    lib_source = ":srcs",
    # pass include/version123 to C/C++ provider as include directory
    out_include_dir = "include/version123",
    out_static_libs = select({
        "//:windows": ["libhello.lib"],
        "//conditions:default": ["libhello.a"],
    }),
)

cc_binary(
    name = "libhello_example_ninja",
    # includes just hello.h, include directory: "include/version123"
    srcs = ["hello_client.c"],
    deps = ["libhello_ninja"],
)

sh_test(
    name = "test_hello_ninja",
    srcs = ["test_hello.sh"],
    args = ["$(location libhello_example_ninja)"],
    data = [":libhello_example_ninja"],
    tags = ["windows"],
    visibility = ["//:__pkg__"],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

# Shows a standard library using the NMake generator
cmake(
    name = "libhello_win_nmake",
    generate_args = ["-G \"NMake Makefiles\""],
    lib_source = ":srcs",
    # pass include/version123 to C/C++ provider as include directory
    out_include_dir = "include/version123",
    out_static_libs = ["libhello.lib"],
    # TODO: Update to `exec_compatible_with`
    tags = [
        "manual",
        "windows",
    ],
)

cc_binary(
    name = "libhello_example_nmake",
    # includes just hello.h, include directory: "include/version123"
    srcs = ["hello_client.c"],
    # TODO: Update to `exec_compatible_with`
    tags = [
        "manual",
        "windows",
    ],
    deps = [":libhello_win_nmake"],
)

sh_test(
    name = "test_hello_nmake",
    srcs = ["test_hello.sh"],
    args = ["$(location libhello_example_nmake)"],
    data = [":libhello_example_nmake"],
    tags = [
        "manual",
        "windows",
    ],
    visibility = ["//:__pkg__"],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

# Finally, show a library with a non-defaulted name
cmake(
    name = "libhello123",
    lib_source = ":srcs",
    out_include_dir = "include",
    # the name of the static library != <name>.a, put it explicitly
    out_static_libs = select({
        "//:windows": ["libhello.lib"],
        "//conditions:default": ["libhello.a"],
    }),
)

cc_binary(
    name = "libhello_example123",
    # includes version123/hello.h, include directory: "include"
    srcs = ["hello_client_version123.c"],
    deps = [":libhello123"],
)
